package net.wenku8.reader

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.core.view.WindowCompat
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import net.wenku8.reader.enums.RouteAccountDetail
import net.wenku8.reader.enums.RouteAppDetail
import net.wenku8.reader.enums.RouteCacheClear
import net.wenku8.reader.enums.RouteFileDetail
import net.wenku8.reader.enums.RouteFileRead
import net.wenku8.reader.enums.RouteHome
import net.wenku8.reader.enums.RouteLogin
import net.wenku8.reader.enums.RouteOnlineDetail
import net.wenku8.reader.enums.RouteOnlineRead
import net.wenku8.reader.enums.RouteOnlineReviewFollow
import net.wenku8.reader.enums.RouteOnlineReviewList
import net.wenku8.reader.enums.RouteRankDetail
import net.wenku8.reader.enums.RouteSearch
import net.wenku8.reader.ui.lite.LiteLoading
import net.wenku8.reader.ui.theme.ReaderTheme
import net.wenku8.reader.view.Home
import net.wenku8.reader.view.file.FileDetail
import net.wenku8.reader.view.file.FileRead
import net.wenku8.reader.view.online.OnlineDetail
import net.wenku8.reader.view.online.OnlineRead
import net.wenku8.reader.view.online.OnlineReviewFollow
import net.wenku8.reader.view.online.OnlineReviewList
import net.wenku8.reader.view.rank.RankDetail
import net.wenku8.reader.view.rank.Search
import net.wenku8.reader.view.user.AccountDetail
import net.wenku8.reader.view.user.AccountDetailViewModel
import net.wenku8.reader.view.user.AppDetail
import net.wenku8.reader.view.user.CacheClear
import net.wenku8.reader.view.user.Login


class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val context = this
        // 取消边缘到边缘模式
        WindowCompat.setDecorFitsSystemWindows(window, true) // 恢复系统栏的默认行为
        setContent {
            val mainViewModel = viewModel<MainViewModel>()
            LaunchedEffect(Unit) {
                mainViewModel.initData(context)
            }
            if (mainViewModel.initSuccess){
                MainComposable()
            }else{
                LiteLoading("初始化中...")
            }
        }
    }
}


val LocalNavController = staticCompositionLocalOf<NavController>{
    error("No NavController provides")
}

@Composable
fun MainComposable(){
    ReaderTheme{
        val navController = rememberNavController()
        CompositionLocalProvider(LocalNavController provides navController) {
            NavHost(navController, startDestination = RouteHome) {
                composable(RouteHome) { Home() }
                composable(RouteLogin) { Login() }
                composable(RouteRankDetail) { RankDetail() }
                composable(RouteOnlineDetail) { OnlineDetail() }
                composable(RouteOnlineRead) { OnlineRead() }
                composable(RouteFileDetail) { FileDetail() }
                composable(RouteFileRead) { FileRead() }
                composable(RouteSearch) { Search() }
                composable(RouteAccountDetail) { AccountDetail() }
                composable(RouteCacheClear) { CacheClear() }
                composable(RouteAppDetail) { AppDetail() }
                composable(RouteOnlineReviewList) { OnlineReviewList() }
                composable(RouteOnlineReviewFollow) { OnlineReviewFollow() }
            }
        }
    }
}
